<?php

namespace App\Controller\Admin\Rbac;

use App\Controller\Admin\BaseController;
use App\Service\Rbac\RbacService;
use App\Validator\Admin\Rbac\RoleValidator;
use Hyperf\Di\Annotation\Inject;

class RoleController extends BaseController
{

    #[Inject]
    protected RoleValidator $roleValidator;

    #[Inject]
    protected RbacService $rbacService;

    public function all()
    {
        $lists = $this->rbacService->allRole();
        return $this->success([
            'lists' => $lists,
        ]);
    }

    public function pageList()
    {

    }

    public function detail()
    {
        $va = $this->roleValidator->detail($this->allParams());
        $routes = $this->rbacService->getAllRoutesOfRole($va['id']);
        return $this->success([
            'routes' => $routes,
        ]);
    }

    public function add()
    {
        $va = $this->roleValidator->add($this->allParams());
        [$r, $msg] = $this->rbacService->addRole($va);
        if ($r) {
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function update()
    {
        $va = $this->roleValidator->update($this->allParams());
        [$r, $msg] = $this->rbacService->updateRole($va['id'], $va);
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function del()
    {
        $va = $this->roleValidator->del($this->allParams());
        [$r, $msg] = $this->rbacService->delRole($va['ids']);
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function bindRoutes()
    {
        $va = $this->roleValidator->bindRoutes($this->allParams());
        [$r, $msg] = $this->rbacService->roleSaveRoute($va['id'], $va['route_ids']);
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

}